package com.animerank.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.animerank.dto.Result;
import com.animerank.entity.UserActivity;
import com.animerank.service.IUserActivityService;
import com.animerank.utils.UserContextHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户活动记录Controller
 */
@RestController
@RequestMapping("/activity")
public class UserActivityController {
    
    @Autowired
    private IUserActivityService userActivityService;
    
    /**
     * 获取当前用户的最新活动记录（支持分页）
     * @param page 页码，默认为1
     * @param pageSize 每页数量，默认为9
     * @return 分页数据，包含活动记录列表和总数
     */
    @GetMapping("/latest")
    public Result<Map<String, Object>> getLatestActivities(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "9") Integer pageSize) {
        // 获取当前登录用户ID
        Long userId = UserContextHolder.getUserId();
        if (userId == null) {
            return Result.fail("用户未登录");
        }
        
        // 获取分页活动记录
        List<UserActivity> activities = userActivityService.getLatestUserActivities(userId, page, pageSize);
        // 获取总数
        int total = userActivityService.getUserActivityCount(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("data", activities);
        result.put("total", total);
        
        return Result.success(result);
    }
}